package com.example.volley;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

/**
 *步骤
 * 1.初始化请求队列 RequestQueue queue
 * 2.创建请求StringRequest,JsonRequest,ImageRequest  request
 *       请求方式 Request.Method.GET...
 * 3.添加请求 queue.add(request);
 * 4.取消请求  在onDestory()方法中设置，需要有Tag
 *          设置Tag    request.setTag(this);
 *          取消请求   queue.cancelAll(this);
 *
 */
public class MainActivity extends AppCompatActivity {

    private String url = "http://www.0790tg.com/appapi/api.php?s=/Tuan/typeList&type=1";
    private String imageUrl = "http://v1.qzone.cc/avatar/201308/30/22/56/5220b2828a477072.jpg%21200x200.jpg";
    private RequestQueue queue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stringRequestMothod();
    }

    private void stringRequestMothod() {
        //初始化请求队列
        queue = Volley.newRequestQueue(this);
        //创建请求
        StringRequest request=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        //设置缓存数据
        request.setShouldCache(true);
        //设置Tag
        request.setTag(this);
        //添加请求
        queue.add(request);
    }

    //取消请求
    @Override
    protected void onDestroy() {
        super.onDestroy();
        queue.cancelAll(this);
    }
}
